<?php
/**
 * Smart Clinic
 *
 ** LICENSE
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code
 * which is considered copyrighted (c) material of the original comment or credit authors.
 *
 * @copyright   2012 Greensun
 * @author      Hoang Minh
 * @version     1.0
 * @since       2012/08/30
 */

/**
 * Customer Management Controller
 * @copyright   2012 Greensun
 * @author      Hoang Minh
 * @version     1.0
 * @since       2012/08/30
 */
class MCategoryController extends AppController  {
	public $helpers = array('Html', 'Form');
	public $uses = array('Category');
	/**
	 *  Action load page default layout
	 */
	public function index() {
		$this -> layout = 'adminlayout';
		$this -> set('title_page', 'Quản lí nhóm');
		$this -> set('title_for_layout', 'Quản lí nhóm');
		$this -> set('type', 'category');
		$categoryList = $this -> Category -> find('all', array('order' => array('toporder' => 'ASC')));
		$this -> set('categoryList', $categoryList);
		$this -> set('message', $_SESSION['message'] == null ? '' : $_SESSION['message']);
		$_SESSION['message'] = null;
	}
	
	public function insert() {
		$this -> layout = 'adminlayout';
		$this -> set('title_page', 'Thêm nhóm mới');
		$this -> set('title_for_layout', 'Thêm nhóm mới');
		$this -> set('type', 'category');
		$this -> set('message', '');
		
		if ($this -> request -> is('post')) {
			$arrData = $_POST['data'];
			$lastCategory = $this -> Category -> find('first', array('order' => array('toporder' => 'DESC')));
			if($lastCategory['Category']['toporder'] == null) {
				$lastCategory['Category']['toporder'] = 0;
			}
			$arrData['Category']['status'] = '1';
			$arrData['Category']['toporder'] = $lastCategory['Category']['toporder'] + 1;
			if($this -> Category -> save($arrData)) {
				$_SESSION['message'] = "Thêm nhóm thành công";
			} else {
				$_SESSION['message'] = "Thêm nhóm thất bại";
			}
			$this -> redirect(array('controller'=>'mcategory', 'action' => 'index'));
		}
	}
	
	public function update() {
		$this -> layout = 'adminlayout';
		$this -> set('title_page', 'Sửa thông tin nhóm');
		$this -> set('title_for_layout', 'Sửa thông tin nhóm');
		$this -> set('type', 'category');
		$this -> set('message', '');
		
		if ($this -> request -> is('post')) {
			$arrData = $_POST['data'];
			if($this -> Category -> save($arrData)) {
				$_SESSION['message'] = "Sửa thông tin nhóm thành công";
			} else {
				$_SESSION['message'] = "Sửa thông tin nhóm thất bại";
			}
			$this -> redirect(array('controller'=>'mcategory', 'action' => 'index'));
		} else {
			$updatedItem = $this -> Category -> find('first', array('conditions' => array('id' => $_GET['id'])));
			$this -> set('updatedItem', $updatedItem);
		}
	}
	
	public function delete() {
		if($this -> Category -> delete(array('id' => $_GET['id']))) {
			$_SESSION['message'] = "Xóa nhóm thành công";
		} else {
			$_SESSION['message'] = "Xóa nhóm thất bại";
		}
		$this -> redirect(array('controller'=>'mcategory', 'action' => 'index'));
	}
	
	public function changeorder() {
		$data = $_POST['data'];
		
		foreach($data as $eachCategory) {
			$this->Category->id = $eachCategory['id'];
    		$this->Category->saveField('toporder', intval($eachCategory['index']));
		}
		echo json_encode("");
		exit();
	}
}